package io.github.talelin.latticy.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.github.talelin.autoconfigure.exception.NotFoundException;
import io.github.talelin.latticy.bo.BannerWithItemsBO;
import io.github.talelin.latticy.dto.BannerDTO;
import io.github.talelin.latticy.mapper.BannerItemMapper;
import io.github.talelin.latticy.mapper.BannerMapper;
import io.github.talelin.latticy.model.BannerDO;
import io.github.talelin.latticy.model.BannerItemDO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BannerService extends ServiceImpl<BannerMapper, BannerDO> {

    @Autowired
    private BannerItemMapper bannerItemMapper;

    public void create(BannerDTO dto) {
        BannerDO bannerDO=new BannerDO();
        BeanUtils.copyProperties(dto,bannerDO);
        this.save(bannerDO);
    }

    public void delete(Integer id) {
        BannerDO banner = this.getById(id);
        if(banner == null) {
            throw new NotFoundException(20000);
        }
        this.getBaseMapper().deleteById(id);
    }

    public void update(BannerDTO dto,Integer id) {
        BannerDO bannerDO = this.getById(id);
        if(bannerDO == null) {
            throw new NotFoundException(20000);
        }
        BeanUtils.copyProperties(dto,bannerDO);
        this.updateById(bannerDO);
    }

    public List<BannerDO> getBanners() {
        return this.baseMapper.getAllBanners();
    }

    public BannerWithItemsBO getWithItems(Integer id) {
        BannerDO banner = this.getById(id);
        if (banner == null) {
            throw new NotFoundException(20000);
        }
        // 构建where条件
        QueryWrapper<BannerItemDO> wrapper = new QueryWrapper<>();
//        wrapper.eq("banner_id",id); // 外键
        // 操作属性
        wrapper.lambda().eq(BannerItemDO::getBannerId,id);
        List<BannerItemDO> bannerItems = bannerItemMapper.selectList(wrapper);
        return new BannerWithItemsBO(banner,bannerItems);
    }

}
